package com.sojson.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.DictTypeParamVerify;
import com.sojson.entity.po.DictType;
import com.sojson.result.Result;
import com.sojson.service.DictTypeService;

import io.swagger.annotations.Api;

/**
 * 字典类型表控制器
 * 
 * @author liu
 * @date 2021-04-13
 */
@Api(tags = "字典类型表管理")
@RestController
@RequestMapping(value = "/dictType")
public class DictTypeController {

    @Resource
    private DictTypeService dictTypeServiceImpl;

    /**
     * 单条插入
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @PostMapping(value = "/insert")
    @PreAuthorize("@ss.hasPermi('/dictType/insert')")
    public Object insert(@RequestBody DictType dictType) throws SQLException {
        // 校验参数
        Result<?> paramResult = DictTypeParamVerify.insert(dictType);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictType.insertInit();
        dictTypeServiceImpl.insert(dictType);
        return Result.success();
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @PutMapping(value = "/updatePartialById")
    @PreAuthorize("@ss.hasPermi('/dictType/updatePartialById')")
    public Object updatePartialById(@RequestBody DictType dictType) throws SQLException {
        // 校验参数
        Result<?> paramResult = DictTypeParamVerify.updatePartialById(dictType);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dictType.updateInit();
        dictType.setUpdateNull();
        dictTypeServiceImpl.updatePartialById(dictType);
        return Result.success();
    }

    /**
     * 通过code列表删除(物理删除)
     * 
     * @param codes
     * @return
     * @throws SQLException
     */
    @DeleteMapping(value = "/deleteByCodeList")
    @PreAuthorize("@ss.hasPermi('/dictType/delete')")
    public Object deleteByCodeList(@RequestParam("codes") ArrayList<String> codes) throws SQLException {
        if (codes.size() == 0) {
            return Result.success();
        }

        return Result.success(dictTypeServiceImpl.deleteByCodeList(codes));
    }

    /**
     * 根据对象查询
     * 
     * @param dictType
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByDto")
    @PreAuthorize("@ss.hasPermi('/dictType/findByDto')")
    public Object findByDto(DictType dictType) throws SQLException {
        List<DictType> findByDto = dictTypeServiceImpl.findByDto(dictType);
        return Result.success(findByDto, findByDto);
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findById")
    public Object findById(Integer id) throws SQLException {
        DictType findById = dictTypeServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过code查询
     * 
     * @param code
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/findByCode")
    public Object findByCode(String code) throws SQLException {
        DictType findByCode = dictTypeServiceImpl.findByCode(code);
        return Result.success(findByCode, findByCode);
    }

}